﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Interfaces
Imports Lourdes.BackEnd.View
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd
Imports Infragistics.Win.UltraWinGrid
Public Class frmPago
  Dim _DocId As Integer
  'Public Sub New()

  '  ' Llamada necesaria para el diseñador.
  '  InitializeComponent()

  '  ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

  'End Sub

  Public Sub New(pDocId As Integer)
    ' Llamada necesaria para el diseñador.
    InitializeComponent()

    ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
    _DocId = pDocId
    ConfigurarGrilla()
  End Sub

  Private Sub ConfigurarGrilla()
    With grdPagos
      .DataSource = (New BC_Pagos).GetByDocId(_DocId)
      .DataBind()
      Dim cont As Integer = 1
      For Each unaFila As Infragistics.Win.UltraWinGrid.UltraGridRow In .Rows
        unaFila.Cells("CuotaId").SetValue(cont, False)
        cont += 1
      Next
    End With
  End Sub

  Private Function GridGetMontoSeleccionado() As Double
    Dim dblMonto As Double = 0
    Dim sel As Boolean = False
    With grdPagos
      For Each unaFila As UltraGridRow In .Rows
        sel = CBool(unaFila.Cells("Seleccionar").Text)
        If sel Then
          dblMonto += CDbl(unaFila.Cells("Total").Text)
        End If
      Next
    End With
    Return dblMonto
  End Function


  Private Sub ActualizarTotal()
    Dim dblTotal As Double = GridGetMontoSeleccionado()
    lblTotal.Text = "Pendiente $ " & dblTotal.ToString("##,##0.00") & ".-"
  End Sub

  Private Sub frmPago_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ActualizarTotal()
  End Sub

  Private Sub grdPagos_CellChange(sender As Object, e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles grdPagos.CellChange
    If e.Cell.Column.Key = "Seleccionar" Then
      ActualizarTotal()

    End If
  End Sub

  Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
    Guardar()
  End Sub

  Private Function GetPagosSel() As List(Of Pagos)
    Dim lstPagos As New List(Of Pagos)
    For Each unaFila In grdPagos.Rows
      If unaFila.Cells("Seleccionar").Value = True Then
        lstPagos.Add(unaFila.ListObject)
      End If
    Next
    Return lstPagos
  End Function

  Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
    If MsgBox("¿Desea salir? Toda la información no guardada se perdera.", vbYesNo, "Salir de Pagos") = vbYes Then
      Me.Dispose()
      Me.Close()
    End If
  End Sub

  Private Sub Guardar()
    Dim lstPagos As List(Of Pagos) = GetPagosSel()
    Dim intReciboId As Integer
    Dim bcPagos As New BC_Pagos
    If lstPagos.Count = 0 Then
      UcStatusBar1.MostrarMensajeRojo("Debe seleccionar al menos un pago.")
    Else
      If MessageBox.Show("¿Esta seguro que desea registrar el pago de $ " & GridGetMontoSeleccionado() & ".-?", "Verificar", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        intReciboId = bcPagos.SetPagoFactura(lstPagos)
        If MessageBox.Show("¿Desea Imprimir el Recibo?", "Verificar", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
          Utils.ShowReport(Enums.ReporteTipos.Recibo, "DocReciboId", intReciboId.ToString, Me)
        End If
        UcStatusBar1.MostrarMensajeVerde("Se a registrado el pago, Recibo Nº: " & intReciboId.ToString & ".")
        btnGuardar.Enabled = False
      End If
    End If
  End Sub

  Private Sub frmPago_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If Keys.F2 = e.KeyCode And btnGuardar.Enabled = True Then
      btnGuardar_Click(Nothing, Nothing)
    ElseIf Keys.Escape = e.KeyCode And btnCancelar.Enabled = True Then
      btnCancelar_Click(Nothing, Nothing)
    End If
  End Sub


End Class